package sol.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * Generate Parentheses
 */
public class Issue022 {
    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<String>();
        generate(res, "", n, n);
        return res;
    }

    private void generate(List<String> res, String current, int left, int right) {
        if (left == 0 && right == 0)
            res.add(current);
        if (left > 0)
            generate(res, current + "(", left - 1, right);
        if (right > left)
            generate(res, current + ")", left, right - 1);
    }

    public static void main(String[] args) {
        System.out.println(new Issue022().generateParenthesis(3));
    }
}
